import { app, BrowserWindow, ipcMain } from "electron";

import setting from "./setting";
import keywords from "./keywords";
import music from "./music";
import barrage from "./barrage";

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== "development") {
	global.__static = require("path")
		.join(__dirname, "/static")
		.replace(/\\/g, "\\\\");
}

let mainWindow;
const winURL =
	process.env.NODE_ENV === "development"
		? `http://localhost:9080`
		: `file://${__dirname}/index.html`;

function createWindow() {
	/**
	 * Initial window options
	 */
	mainWindow = new BrowserWindow({
		width: 360,
		height: 380,
		useContentSize: true,
		// 双击放大
		maximizable: false,
		// 背景透明
		transparent: true,
		// 是否可缩放
		resizable: false,
		// 是否去除外边框
		frame: false,
		// 是否置顶
		alwaysOnTop: true,
		//全屏展示
		// fullscreen: true,
		// 窗口居中
		center: true,
		webPreferences: {
			// 是否打开调试控制台
			// devTools: false,
			enablemotemodule: true,
			nodeIntegration: true,
			// 不拦截跨域
			webSecurity: false,
		},
	});

	mainWindow.loadURL(winURL);

	mainWindow.on("closed", () => {
		mainWindow = null;
	});
}

// 打开setting子窗口
ipcMain.on("open-setting", () => {
	setting();
});

// 打开keywords子窗口
ipcMain.on("open-keywords", () => {
	keywords();
});

// 打开点歌姬子窗口
ipcMain.on("open-music", () => {
	music();
});

// 打开弹幕姬
ipcMain.on("open-barrage", () => {
	barrage();
});

// 关闭窗口
ipcMain.on("window-close", () => {
	mainWindow.close();
});

// 置顶 || 取消置顶
ipcMain.on("window-top", (e, val) => {
	mainWindow.setAlwaysOnTop(val);
	BrowserWindow.getFocusedWindow().webContents.send("alwaysOnTop", val);
});

// 穿透 || 取消穿透
ipcMain.on("window-penetrate", (e, ...val) => {
	mainWindow.setIgnoreMouseEvents(...val);
});

app.on("ready", createWindow);

app.on("window-all-closed", () => {
	if (process.platform !== "darwin") {
		app.quit();
	}
});

app.on("activate", () => {
	if (mainWindow === null) {
		createWindow();
	}
});

// 请求重置请求头
app.on("browser-window-created", (e, win) => {
	win.webContents.session.webRequest.onBeforeSendHeaders((detail, cb) => {
		let { url, requestHeaders } = detail;
		requestHeaders = Object.assign(requestHeaders, {
			"User-Agent":
				"Mozilla/5.0 (Linux; Android 9; PCT-AL10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.311 Mobile Safari/537.36",
			Referer: requestHeaders.referer,
			cookie: requestHeaders.myCookie,
		});
		cb({ requestHeaders });
	});
});

// 退出的时候，注销所有的快捷键
app.on("will-quit", function() {
	globalShortcut.unregisterAll();
});

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
